import geom.FacetList;
import geom.Polygon;
import geom.PolygonList;
import geom.Solid;
import inset.GraphViewer;
import inset.InsetProcessor;
import inset.StraightSkeleton;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.io.FileNotFoundException;

import javax.media.j3d.BoundingBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.vecmath.Point3d;

import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;

import nativ.ContourGenerator;


public class skeleton
{

    /**
         * @param args
     * @throws ParsingErrorException 
     * @throws IncorrectFormatException 
     * @throws FileNotFoundException 
         */
    public static void main(String[] args) throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
    {
	System.out.println("Creating polygons");
	// Polygon poly = new Polygon(new double[][]{ {1, 0},
      //   {5,0},{7,1},{6,6},{0,6}, {0, 1}});
	 //Polygon poly = new Polygon(new double[][]{ {0,0}, {5, 9.9}, {10,0},
       // {10,10}, {0,10} });
	/*Polygon poly = new Polygon(new double[][] { { 0, 10 }, { 10, 10 },
		{ 10, 0 }, { 5, 9.9 }, { 0, 0 } });
	Polygon sub = new Polygon(new double[][] { { 2, 7 }, { 2, 8 },
		{ 3, 8 }, { 3, 7 } });
	Polygon sub2 = new Polygon(new double[][] { { 7, 7 }, { 8, 7 },
		{ 8, 9 }, { 7, 9 } });
	Polygon poly2 = new Polygon(new double[][]{ {2, 0}, {2,2}, {8,2}, {8,0}});
	
	Polygon Ashape = new Polygon(new double[][]{ {0,10}, {1,10}, {3,7}, {8,7}, {9,10},{10,10},{5.5,0},{4.5,0}});
	Polygon Ahole  = new Polygon(new double[][]{ {3,6}, {5,1}, {7,6}});
    
    //Polygon poly = new Polygon(new double[][]{ {0,0}, {10,0}, {10,7}, {3,7}, {0,10} });

	PolygonList list = new PolygonList();*/
	
	//list.add(poly);
	//list.add(sub);
//	list.add(sub2);
	//list.add(poly2);
	
	//list.add(Ashape);
	//list.add(Ahole);
	
	StlFile loader = new StlFile();
	Solid scene = loader.load("logo.stl");
	FacetList fl = loader.generateFacetList();
    
    double dist = -5;
    
    PolygonList list = fl.getSliceForZ(fl.getLineSetsForZ(dist));
    PolygonList insets = ContourGenerator.generateContour(list);
    //PolygonList insets = new InsetProcessor(list.calculateInsetPolygons(dist)).getInset();
	
	System.out.println(insets.get(0).getLineSegments().size());
	
	final JFrame f = new JFrame();
	final JLabel pos = new JLabel();
	f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	ShapeCanvas sc = new ShapeCanvas();
	sc.addMouseMotionListener(new MouseMotionListener()
	{
	    public void mouseDragged(MouseEvent e)
	    {
	    }

	    public void mouseMoved(MouseEvent e)
	    {
		pos.setText((e.getX() - 100) / 30.0 + "," + (e.getY() - 100)
			/ 30.0);
	    }

	});

	f.getContentPane().setLayout(new BorderLayout());
	f.getContentPane().add(sc, BorderLayout.CENTER);
	f.getContentPane().add(pos, BorderLayout.SOUTH);

	AffineTransform t = AffineTransform.getTranslateInstance(100, 100);
	t.scale(30, 30);
	sc.setShape(t.createTransformedShape(list.getShape()));
	sc.setInset(t.createTransformedShape(insets.getShape()));    
    //sc.setBisectors(t.createTransformedShape(bisectors));

	f.setSize(600, 600);
	f.setVisible(true);
    }

    static class ShapeCanvas extends JPanel
    {
	public void setShape(Shape s)
	{
	    shapeToPaint = s;
	}

	public void setInset(Shape s)
	{
	    inset = s;
	}
    
    public void setBisectors(Shape s)
    {
        bisectors = s;
    }

	public void paint(Graphics g)
	{
	    super.paint(g);

	    Graphics2D g2 = (Graphics2D) g;

	    if (shapeToPaint != null)
	    {
	    	g2.draw(shapeToPaint);
	    	//drawAnimated(shapeToPaint, g2);
	    }

	    if (inset != null)
	    {
		// g2.setStroke(new BasicStroke(10.0f));
		g2.setColor(Color.BLUE);
		g2.draw(inset);
        //drawIterOrder(inset, Color.BLACK, g2);
	    }
        
        if (bisectors != null)
        {
            g2.setColor(Color.RED);
            g2.draw(bisectors);
        }

	}
	
	private void drawAnimated(Shape s, Graphics2D g)
	{
		PathIterator pi = s.getPathIterator(null);        
        
        float cur[] = new float[6], old[] = new float[6];
        
        if (!pi.isDone())
        	pi.currentSegment(old);
        
        while (!pi.isDone())
        {
            int type = pi.currentSegment(cur);
            
            switch (type)
            {
            	case PathIterator.SEG_LINETO:
            	{
            		g.drawLine(Math.round(old[0]), Math.round(old[1]), Math.round(cur[0]), Math.round(cur[1]));
            		break;            		
            	}
            }
            
            System.arraycopy(cur, 0, old, 0, cur.length);
            
            try
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
	}
    
    private void drawIterOrder(Shape s, Color c, Graphics2D g)
    {
        PathIterator pi = s.getPathIterator(null);
        int i = 1;
        
        float coords[] = new float[6];
        while (!pi.isDone())
        {
            pi.currentSegment(coords);
            
            g.drawString(i + " ", coords[0], coords[1]);
            i++;
            pi.next();
            pi.next();
        }
    }

	private Shape shapeToPaint, inset, bisectors;

    }
}
